/*
 * kylin-os-manager
 *
 * Copyright (C) 2022, KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "dnscheck.h"
#include <QtConcurrent>
#include <QThread>
#include <QEventLoop>
#include <QUrl>

DNSCheck::DNSCheck(QObject *parent) : QObject(parent)
{
    // set plugin type
    setPluginType(CheckType::DNS_CONF);
    m_cur.m_index = static_cast<int>(CheckType::DNS_CONF);
    m_checkTool = NWCheckToolLib::getInstance();
    m_cur.m_projectName = QString(tr("DNS Config"));
    m_cur.m_projectDetail = QString(tr("Are DNS config right?"));
    setCheckKey(getKeyInfoPtr()->getKey());
    setProjectName((tr("DNS Config")).toStdString());
}
DNSCheck::~DNSCheck()
{
    if (mDnsCheckThread) {
        if (mThread && mThread->isRunning()) {
            mThread->quit();
            mThread->wait();
        }
        delete mDnsCheckThread;
    }
}
//返回检测结果是否OK
// yyl 为修复eventloop的bug   该方法的返回值已经没有任何意义了
CHECKRESULT DNSCheck::getCheckResult()
{
    qDebug() << "=====================dnscheck thread:" << QThread::currentThreadId() << "=====================";
    return CHECKRESULT::NO_DNS;
}

//开始检测
void DNSCheck::startChecking(InnerNetCheck &checkSettings)
{
    m_settings = checkSettings;
    qDebug() << "DNSCheck::startChecking " << checkSettings.ip;
    qDebug() << "DNSCheck::startChecking " << checkSettings.web;
    m_cur.setStatusCheck(CheckStatus::CHECKING);
    m_cur.setCurInfo(tr("Checking DNS config"), tr("Checking"));
    Notify(m_cur);

    qDebug() << "DNSCheck::startChecking be start check    currentThread:" << QThread::currentThreadId();
    if (!mDnsCheckThread) {
        mThread = new QThread(this);
        mDnsCheckThread = new DnsCheckThread();
        connect(this, &DNSCheck::sigDnsCheckIsStart, mDnsCheckThread, &DnsCheckThread::slotDnsCheckStart);
        connect(mDnsCheckThread, &DnsCheckThread::sigDnsCheckIsOver, this, &DNSCheck::slotDnsCheckIsOver);
        mDnsCheckThread->moveToThread(mThread);
        mThread->start();
    }
    emit sigDnsCheckIsStart(m_settings);
}

void DNSCheck::slotDnsCheckIsOver(statusStruct cur)
{
    qDebug() << "DNSCheck::slotCheckIsOver currentThread:" << QThread::currentThreadId();
    QString projectDetail = cur.m_projectDetail;
    QString projectRes = cur.m_projectRes;
    m_cur.setCurInfo(projectDetail, projectRes);
    m_cur.setStatusCheck(cur.m_curStutus);

    //防止检测过快
    QEventLoop loop;
    QTimer::singleShot(1000, &loop, [=]() {
        Notify(m_cur);
    });
    loop.exec();
}

void DNSCheck::setInit()
{
    m_cur.m_curStutus = CheckStatus::INIT;
    Notify(m_cur);
}
